// 全局请求封装——也可像上述一样根据环境不同的判断
// const base_url = 'http://39.107.247.62:48080/app-api/';
 const base_url = 'https://ywtmp.hnwytkj.com/app-api/';//开发环境
// const base_url = '你的测试环境url';//测试环境
// const base_url = '你的正式环境url';//正式环境
// 请求超时设置
const timeout = 30000;
export default function request(params) {
	let url = params.url;
	let method = params.method || "get";
	let data = params.data || {};
	let header = {
		'Authorization': uni.getStorageSync('token') || '',
		'Content-Type': 'application/json;charset=UTF-8',
		...params.header
	};
 
 
 
	if (method === "post") {
		header['Content-Type'] = 'application/json';
	}
 
	return new Promise((resolve, reject) => {
		uni.request({
			url: base_url + url,
			method: method,
			header: header,
			data: data,
			timeout,
			success(response) {
				console.log(response, '响应')
				const {
					statusCode,
					data,
					errMsg
				} = response;
				if (statusCode == 200) {
					if ((data.code === 0) || (params.url.indexOf('card/user-profile/get/recommender') > -1)) {
						resolve(data);//需要根据后端实际接口返回数据层级去resolve
						// resolve(data);
					}
					else {
						// uni.clearStorageSync();
						switch (data.code) {
							case 401:
								// uni.navigateTo({
								// 	url: "/pages/login/index",
								// });
								const token = uni.getStorageSync('token')
								const refreshToken = uni.getStorageSync('refreshToken')
								if (token && refreshToken) {
									if (token !== refreshToken) {
										uni.setStorageSync('token', refreshToken);
										request(params)
									} else {
										uni.removeStorageSync('token')
										uni.removeStorageSync('refreshToken')
										uni.removeStorageSync('userId')
									}						
								} else {
									uni.removeStorageSync('token')
									uni.removeStorageSync('refreshToken')
									uni.removeStorageSync('userId')
								}
								break;
							case 404:
								uni.showToast({
									title: '请求地址不存在...',
									duration: 3000,
								});
								break;
							default:
								uni.showToast({
									icon: "error",
									title: data.msg,
									duration: 3000,
								});
								break;
						}
					}
				} else if (statusCode == 401) {
					// uni.navigateTo({
					// 	url: "/pages/login/login",
					// });
					const token = uni.getStorageSync('token')
					const refreshToken = uni.getStorageSync('refreshToken')
					if (token && refreshToken) {
						if (token !== refreshToken) {
							uni.setStorageSync('token', refreshToken);
							request(params)
						} else {
							uni.removeStorageSync('token')
							uni.removeStorageSync('refreshToken')
							uni.removeStorageSync('userId')
						}						
					} else {
						uni.removeStorageSync('token')
						uni.removeStorageSync('refreshToken')
						uni.removeStorageSync('userId')
					}
				} else {
					uni.showToast({
						icon: "error",
						title: data.msg,
						duration: 3000,
					});
				}
			},
			fail(err) {
				console.log(err, '网络超时');
				uni.showToast({
					title: err.msg.indexOf('request:fail') !== -1 ? '网络连接失败' : '网络连接失败',
					icon: "error",
					duration: 3000
				});
				reject(err);
			},
			complete() {
				// 不管成功还是失败都会执行
				// uni.hideLoading();
				// uni.hideToast();
			}
		});
	});
};